

#include "game.h"
#include "stdlib.h"
#include "bmp.h"
#include "mainpage.h"

extern uint16_t BBscale[16]; 

extern uint8_t mode;

bird_t mybird = {.x = 30, .y = 32, .yv = 0};
barrier_t barriers[4] = {{128, 45}, {160, 30}, {192, 20}, {224, 25}};
static uint8_t score = 0;
static uint8_t failedflag = 0;

void drawfailpic(uint16_t __score) //游戏失败界面
{
    BBstop();
    char failbuf[20];
    sprintf(failbuf, "Score:%d", __score);
    u8g2_SetFont(&oled, u8g2_font_profont12_mr);
    u8g2_FirstPage(&oled);
    do
    {
        if (__score != 22) //人不能看见看不见的东西
            u8g2_DrawStr(&oled, 24, 27, failbuf);
        u8g2_DrawStr(&oled, 0, 45, "KEY1 Restart");
        u8g2_DrawStr(&oled, 0, 60, "KEY2 Back to menu");

    } while (u8g2_NextPage(&oled));
}

static void resetgame(void)
{
    // score = 0;
    mybird.y = 32;
    mybird.yv = 0;
    for (int i = 0; i < 4; i++)
    {
        barriers[i].x = 128 + 32 * i;
        // srand(Get_Systick());
        uint8_t rn = Get_Systick() % 27;
        barriers[i].y = rn + 18;
        if (barriers[i].y > 45)
            barriers[i].y = 45;
        HAL_Delay(10);
    }
}

void Flappy_Bird(void)
{
    static uint8_t pauseflag = 0;
    static uint8_t frame = 0;

    //如果按下KEY2键则退出游戏,将障碍和鸟重置
    if (GetKeyVal(18)) //
    {
        frame = 0;
        resetgame();
        score = 0;
        gamestart = 0;
        failedflag = 0;
        mode = 0;
        return;
    }

    //如果没有失败则正常运行程序
    if (!failedflag && !pauseflag)
    {
        if (frame < 30)
            frame++;

        mybird.yv += 0.2; //模拟重力加速度

        if (GetKeyVal(4)) //如果按下按键SW4则给一个短时向上的加速度
        {
            BBfrec(BBscale[3]);
            mybird.yv -= 0.8;
        }
        else
        BBstop();
        
        if (mybird.yv > 2)
            mybird.yv = 2;
        if (mybird.yv < -2)
            mybird.yv = -2;
        mybird.y += mybird.yv;

        if (mybird.y <= 0)
            mybird.y = 0;
        if (mybird.y >= 63 - 8)
            mybird.y = 63 - 8;

        for (int i = 0; i < 4; i++)
        {
            barriers[i].x -= 2;
            //鸟越过一个障碍就加一分
            if (barriers[i].x == 28)
                score++;
            if (barriers[i].x > 250)
            {
                barriers[i].x = 128;
                // srand(Get_Systick());
                uint8_t rn = (Get_Systick() / 20 / 7) % 10;
                if (rn % 2)
                    barriers[i].y += rn;
                else
                    barriers[i].y -= rn;
                if (barriers[i].y > 45)
                    barriers[i].y -= 2 * rn;
                if (barriers[i].y < 18)
                    barriers[i].y += 2 * rn;
            }
        }

        char scorebuf[4];
        sprintf(scorebuf, "%d", score);
        u8g2_SetFont(&oled, u8g2_font_profont12_mr);
        u8g2_FirstPage(&oled);
        do
        {
            //画一个猫猫头
            u8g2_DrawXBMP(&oled, mybird.x, mybird.y, 11, 8, flycat);
            //分数
            u8g2_DrawStr(&oled, 0, 15, scorebuf);
            for (int i = 0; i < 4; i++)
            {
                u8g2_DrawLine(&oled, barriers[i].x, (uint8_t)barriers[i].y - 15, barriers[i].x, 0);
                u8g2_DrawLine(&oled, barriers[i].x, (uint8_t)barriers[i].y + 15, barriers[i].x, 127);
            }
            if (frame < 30)
            {
                u8g2_DrawStr(&oled, 50, 36, "Press SW4!");
            }
        } while (u8g2_NextPage(&oled));
        for (int i = 0; i < 4; i++)
        {
            if (barriers[i].x >= 34 && barriers[i].x <= 39)
            {
                if ((barriers[i].y - 15 > mybird.y + 2) || (barriers[i].y + 15 < mybird.y + 7))
                {
                    frame = 0;
                    failedflag = 1;
                    resetgame();
                    drawfailpic(score);
                }
            }
        }
    }
    else if (failedflag)
    {
        if (GetKeyVal(18)) //如果按下B则返回主界面
        {
            frame = 0;
            score = 0;
            gamestart = 0;
            mode = 0;
            failedflag = 0;
        }
        if (GetKeyVal(17)) //如果按下A键则重新开始
        {
            frame = 0;
            score = 0;
            failedflag = 0;
        }
    }
}
